package com.atguigui.leetcode;

/**
 * 11.盛最多水的容器
 * Project: leetcode
 * Package: com.atguigui.leetcode
 * Version: 1.0
 * <p>
 * Created by WJX on 2022/6/7 10:03
 */
public class P11ContainerWithMostWater {
    public static void main(String[] args) {
        Solution solution = new P11ContainerWithMostWater().new Solution();
        // TO TEST|
        int[] in = {1, 8, 6, 2, 5, 4, 8, 3, 7};
        solution.maxArea1(in);
    }

    //leetcode submit region begin(Prohibit modification and deletion)
    class Solution {

        /**
         * 暴力破解法
         *
         * @param height
         * @return
         */
        public int maxArea(int[] height) {

            int result = -1;

            for (int i = 0; i < height.length; i++) {

                for (int j = i + 1; j < height.length; j++) {
                    result = Math.max(result, (j - i) * Math.min(height[i], height[j]));
                }

            }


            return result;
        }


        /**
         * 双指针
         *
         * @param height
         * @return
         */
        public int maxArea1(int[] height) {
            int result = 0;
            int l = 0;
            int r = height.length - 1;

            while (l < r) {
                result = Math.max(result, (r - l) * Math.min(height[l], height[r]));
                if (height[l] <= height[r]) {
                    l++;
                } else {
                    r--;
                }
            }


            return result;
        }
    }
}
